/*******************************************************************************					

	DESCRIPTION: 	This do file creates a dataset with the level of unemployment benefits.
	
*******************************************************************************/

clear all
global id_code 001_7

********************************************************************************
* A1: Combine data from all years
********************************************************************************

use "${IAF}/IAF_Utbetalning_ny.dta", clear

* Rename variables
rename Person_LopNr LopNr_PersonNr
rename utbetdatum datePayment
rename dagpeng benefitsDaily
rename ersgrunddaglon wageBeforeDaily

keep LopNr_PersonNr benefitsDaily wageBeforeDaily datePayment

* Generate a month and year variable
gen date=date(datePayment,"YMD")
format date %td
drop datePayment
gen year=year(date)
gen month=month(date)
gen day=day(date)

* Destring the relevant variables

* Before we can do that we need to modify the variables

* First replace NULL with missing
replace benefitsDaily="." if benefitsDaily=="NULL" // 2 649 to missing
replace wageBeforeDaily="." if wageBeforeDaily=="NULL" // 2 499 to missing

* Then replace the commas with full-stops
gen benefitsDaily2=subinstr(benefitsDaily,",",".",.) // 2 828 to missing
gen wageBeforeDaily2=subinstr(wageBeforeDaily,",",".",.) // 13 747 to missing

destring benefitsDaily2 wageBeforeDaily2, replace

drop benefitsDaily wageBeforeDaily
rename benefitsDaily2 benefitsDaily
rename wageBeforeDaily2 wageBeforeDaily

save "${data_intermediate}/${id_code}_UnempBenefits_clean.dta", replace

duplicates drop // 78 116 245 observations dropped

sort LopNr_PersonNr year month day

* Generate  a variable indicating the inital level of benefits in a month
by LopNr_PersonNr year month: gen initialBenefitsTemp=(_n==1)
gen initialBenefitsTemp2=benefitsDaily if initialBenefitsTemp==1
by LopNr_PersonNr year month: egen initialBenefits=mean(initialBenefitsTemp2)
drop initialBenefitsTemp initialBenefitsTemp2

* Generate  a variable indicating the inital level of previous wage in a month
by LopNr_PersonNr year month: gen initialWageBeforeDailyTemp=(_n==1)
gen initialWageBeforeDailyTemp2=wageBeforeDaily if initialWageBeforeDailyTemp==1
by LopNr_PersonNr year month: egen initialWageBeforeDaily=mean(initialWageBeforeDailyTemp2)
drop initialWageBeforeDailyTemp initialWageBeforeDailyTemp2

keep Lop* year month initial*

* Keep one observation per person in the same year and month
duplicates drop // 20 132 973 observations dropped
duplicates report LopNr_PersonNr year month // no duplicates
misstable summarize
gen replacRatio=initialBenefits/initialWageBeforeDaily

keep LopNr_PersonNr year month replacRatio 
misstable summarize

save "${data_intermediate}/${id_code}_UnempBenefits_clean.dta", replace

use "${data_intermediate}/${id_code}_UnempBenefits_clean.dta", clear


* Generate a variable for every person in the PES dataset
* for years 1999-2014, these are available for replacement rate data
use "${data}/001_1_UnemploymentSpells.dta", clear
keep Lop*
duplicates drop
expand 16
bysort Lop*: gen year = _n + 1998
expand 12
bysort Lop* year: gen month = _n
merge 1:1 Lop* year month using "${data_intermediate}/${id_code}_UnempBenefits_clean.dta"
drop _merge
save "${data_intermediate}/${id_code}_UnempBenefits_clean_alt.dta", replace


use year month Lop* replacRatio using "${data_intermediate}/${id_code}_UnempBenefits_clean_alt.dta", clear

generate time=ym(year, month)

* Generate a lead for the replacement ratio
xtset Lop* time 
forval i = 1/6 {
gen F`i'_replacRatio=F`i'.replacRatio
}

* Generate a new variables that combines the replacement ration and its lead
gen replacRatio_new=replacRatio

forval i = 1/6 {
replace replacRatio_new=F`i'_replacRatio if missing(replacRatio_new)
}

save "${data_intermediate}/${id_code}_UnempBenefits_clean_alt.dta", replace
